﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Globalization;
using System.Text.RegularExpressions;
using System.Diagnostics;


public static class ExtentionMethods
{
    [DebuggerStepThrough]
    public static Boolean IsNumeric(this object Expression)
    {
        if (Expression == null || Expression is DateTime)
            return false;

        if (Expression is Int16 || Expression is Int32 || Expression is Int64 || Expression is Decimal || Expression is Single || Expression is Double || Expression is Boolean)
            return true;

        double result;
        return double.TryParse(Expression.ToString(), System.Globalization.NumberStyles.Any, NumberFormatInfo.InvariantInfo, out result);
    }

    [DebuggerStepThrough]
    public static string RemoveFirst(this string s)
    {
        return s.Length > 0 ? s.Remove(0, 1) : s;
    }

    [DebuggerStepThrough]
    public static string RemoveLast(this string s)
    {
        return s.Length > 0 ? s.Remove(s.Length - 1) : s;
    }

    [DebuggerStepThrough]
    public static string LeftOf(this string s, string pattern)
    {
        int i = s.IndexOf(pattern);
        return i == -1 ? string.Empty : s.Substring(0, i);
    }

    [DebuggerStepThrough]
    public static string RightOf(this string s, string pattern)
    {
        int i = s.IndexOf(pattern);
        return i == -1 ? string.Empty : s.Substring(i + pattern.Length, s.Length - i - pattern.Length);
    }

    [DebuggerStepThrough]
    public static string Left(this string s, int count)
    {
        return s.Substring(0, count);
    }

    [DebuggerStepThrough]
    public static string Right(this string s, int count)
    {
        return s.Substring(s.Length - count, count);
    }

    [DebuggerStepThrough]
    public static string Mid(this string s, int index, int count)
    {
        return s.Substring(index, count);
    }

    [DebuggerStepThrough]
    public static bool ContainsAny(this string s, List<string> lst)
    {
        return lst.Any(item => s.Contains(item));
    }

    [DebuggerStepThrough]
    public static string Match(this string s, string regex)
    {
        return new Regex(regex).Match(s).Value;
    }

    [DebuggerStepThrough]
    public static string Match(this string s, string regex, int startat)
    {
        return new Regex(regex).Match(s, startat).Value;
    }

    [DebuggerStepThrough]
    public static MatchCollection Matches(this string s, string regex)
    {
        return new Regex(regex).Matches(s);
    }

    [DebuggerStepThrough]
    public static MatchCollection Matches(this string s, string regex, int startat)
    {
        return new Regex(regex).Matches(s, startat);
    }
}
